package com.loong.order.controller.forehead;

import com.alibaba.fastjson.JSONObject;
import com.loong.common.http.OKHttpUtil;
import com.loong.common.mvc.BaseForeheadController;
import com.loong.common.param.BaseIdParam;
import com.loong.common.rest.Result;
import com.loong.global.dto.UserDTO;
import com.loong.order.common.forehead.param.OrderBaseForeheadParam;
import com.loong.order.common.forehead.param.OrderDeliveryForeheadParam;
import com.loong.order.common.forehead.param.OrderForeheadParam;
import com.loong.order.common.forehead.param.OrderItemForeheadParam;
import com.loong.order.handler.OrderHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.stream.LongStream;

/**
 * @Description:
 * @Author: hzp
 * @Date: 2020-12-31 9:24
 */
@RestController
@RequestMapping("forehead/order/order")
@Slf4j
public class OrderController extends BaseForeheadController {
    @Resource
    private OrderHandler orderHandler;
    @Resource
    private OKHttpUtil okHttpUtil;

    /**
     * 保存订单
     * @return
     */
    @PostMapping("saveOrder")
    public Result save(@RequestBody OrderForeheadParam param){
        //设置买家用户id
        UserDTO userDTO = getUserDTO();
        param.getOrderBaseForeheadParam().setUserId(userDTO.getId());
       return handlerExecutor.execute(()->orderHandler.save(param));
    }

    /**
     *  提交订单
     * @param param 订单信息
     * @return 是否成功
     */
    @PostMapping("submitOrder")
    public Result submitOrder(@RequestBody OrderForeheadParam param){
        //设置买家用户id
//        UserDTO userDTO = getUserDTO();
//        param.getOrderBaseForeheadParam().setUserId(userDTO.getId());
        return handlerExecutor.execute(()->orderHandler.submitOrder(param));
    }

    /**
     *
     * @param param id 参数
     * @return OrderDetailForeheadResult
     */
    @PostMapping("findOrderDetail")
    public Result findOrderDetail(@RequestBody BaseIdParam param){
        return handlerExecutor.execute(()->orderHandler.findOrderDetail(param.getId()));
    }


    /**
     * todo 待测试==========================
     * 模拟100个人 shopId 10 skuId 50 每个人买20个 库存1000 的场景
     *  todo 待测试==========================
     */
    @PostMapping("testSubmitOrder")
    public void test2(){
        log.debug("============进入testRequest===============");
        String url = "http://forehead.loong.ph/api/forehead/order/order/submitOrder";

        LongStream.rangeClosed(1, 100).
                forEach(i->new Thread(String.valueOf(i)){
                    OrderForeheadParam param = OrderForeheadParam.builder()
                            .orderBaseForeheadParam(OrderBaseForeheadParam.builder().userId(i).build())
                            .orderDeliveryForeheadParam(OrderDeliveryForeheadParam.builder().orderNumber("123456").build())
                            .orderItemList(Arrays.asList(OrderItemForeheadParam.builder().goodsSkuId(50L).num(20).build()))
                            .build();
                    public void run() {
                        //模拟客户端请求抢单
                        okHttpUtil.postJSON(url, JSONObject.toJSONString(param));
                    }
                }.start());

    }
}
